package com.kamistoat.ruoyi.ruoyimallproduct.controller;

import com.kamistoat.ruoyi.ruoyicommoncore.web.domain.AjaxResult;
import com.kamistoat.ruoyi.ruoyicommonsecurity.annotation.RequiresPermissions;
import com.kamistoat.ruoyi.ruoyimallproduct.entity.CategoryEntity;
import com.kamistoat.ruoyi.ruoyimallproduct.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 获取商品菜单的树型结构
     */
    @RequestMapping("/list/tree")
    @RequiresPermissions("mall:category:listTree")
    public AjaxResult listTree(){
        List<CategoryEntity> categoryEntities = categoryService.listWithTree();
        return AjaxResult.success().put("allcategory", categoryEntities);
    }


    /**
     * 查询单条数据信息。这里就是查询单条菜单信息
     */
    @RequestMapping("/info/{catId}")
    @RequiresPermissions("mall:category:info")
    public AjaxResult info(@PathVariable("catId") Long catId) {
        CategoryEntity category = categoryService.getById(catId);
        return AjaxResult.success().put("singlecategory", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("mall:category:save")
    public AjaxResult save(@RequestBody CategoryEntity category) {
        categoryService.save(category);
        return AjaxResult.success();
    }

    /**
     * 拖拽修改。拖拽修改只是在修改排序/父子关系。
     * 通过控制前端，可以在拖拽修改后，只传递新的sort/parentId，这样后端直接更新其sort/parentId即可
     */
    @RequestMapping("/update/sort")
    @RequiresPermissions("mall:category:update")
    public AjaxResult updateSort(@RequestBody CategoryEntity[] category) {
        // 自动生成的Dao中具有按列表修改的功能
        // 接收collection类输入，使用Array.toList将列表转换成collection
         categoryService.updateBatchById(Arrays.asList(category));
        return AjaxResult.success();
    }

    /**
     * 对详细信息的修改。
     * 牵扯到对品牌的冗余修改，暂不实现
     */
    @RequestMapping("/update")
    @RequiresPermissions("mall:category:update")
    public AjaxResult update(@RequestBody CategoryEntity category) {
        // TODO 冗余更新
        // categoryService.updateDetail(category);
        return AjaxResult.success();
    }

    /**
     * 删除
     * 同样牵扯冗余修改，暂不实现
     */
    @RequestMapping("/delete")
    @RequiresPermissions("mall:category:delete")
    public AjaxResult delete(@RequestBody Long[] catIds) {
        // TODO 冗余更新
        // categoryService.removeMenuByIds(Arrays.asList(catIds));
        return AjaxResult.success();
    }

}
